#include "data_list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ===== 数据链表实现 ===== */

void init_data_list(DataList* list) {
    list->head = NULL;
    list->count = 0;
}

int add_data_record(DataList* list, const char* ip, const SensorData* sensor_data) {
    if (list->count >= MAX_RECORDS) return -1;

    DataNode* existing = find_data_record(list, ip);
    if (existing != NULL) return -1;

    DataNode* new_node = (DataNode*)malloc(sizeof(DataNode));
    if (!new_node) return -1;

    strncpy(new_node->ip_address, ip, sizeof(new_node->ip_address) - 1);
    new_node->ip_address[sizeof(new_node->ip_address) - 1] = '\0';
    memcpy(&new_node->data, sensor_data, sizeof(SensorData));
    new_node->next = list->head;
    list->head = new_node;
    list->count++;
    
    return 0;
}

int delete_data_record(DataList* list, const char* ip) {
    DataNode *current = list->head, *prev = NULL;
    
    while (current != NULL) {
        if (strcmp(current->ip_address, ip) == 0) {
            if (prev == NULL) {
                list->head = current->next;
            } else {
                prev->next = current->next;
            }
            free(current);
            list->count--;
            return 0;
        }
        prev = current;
        current = current->next;
    }
    return -1;
}

DataNode* find_data_record(DataList* list, const char* ip) {
    DataNode* current = list->head;
    while (current != NULL) {
        if (strcmp(current->ip_address, ip) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

void print_all_data_records(const DataList* list) {
    printf("Data Records (%d):\n", list->count);
    // printf("IP Address\t\tSensor ID\tTemp\tHumid\tTimestamp\n");
    
    DataNode* current = list->head;
    while (current != NULL) {
        printf("%-15s\t%d\t%d\t%.1f\t%.1f\t%lu\t%ld\n", 
               current->ip_address,
               current->data.sensorid,
               current->data.sensorname,
               current->data.temperature,
               current->data.humidity,
               current->data.timestamp,
               current->data.gdata);
        current = current->next;
    }
}

void clear_data_list(DataList* list) {
    DataNode* current = list->head;
    while (current != NULL) {
        DataNode* temp = current;
        current = current->next;
        free(temp);
    }
    list->head = NULL;
    list->count = 0;
}

/* ===== 命令链表实现 ===== */

void init_cmd_list(CmdList* list) {
    list->head = NULL;
    list->count = 0;
}

int add_cmd_record(CmdList* list, const char* command) {
    if (strlen(command) >= MAX_CMD_LENGTH) return -1;

    CmdNode* new_node = (CmdNode*)malloc(sizeof(CmdNode));
    if (!new_node) return -1;

    strncpy(new_node->command, command, MAX_CMD_LENGTH - 1);
    new_node->command[MAX_CMD_LENGTH - 1] = '\0';
    new_node->next = list->head;
    list->head = new_node;
    list->count++;
    
    return 0;
}

int delete_cmd_record(CmdList* list, const char* command) {
    CmdNode *current = list->head, *prev = NULL;
    
    while (current != NULL) {
        if (strcmp(current->command, command) == 0) {
            if (prev == NULL) {
                list->head = current->next;
            } else {
                prev->next = current->next;
            }
            free(current);
            list->count--;
            return 0;
        }
        prev = current;
        current = current->next;
    }
    return -1;
}

CmdNode* find_cmd_record(CmdList* list, const char* command) {
    CmdNode* current = list->head;
    while (current != NULL) {
        if (strcmp(current->command, command) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

void print_all_cmd_records(const CmdList* list) {
    printf("Command Records (%d):\n", list->count);
    CmdNode* current = list->head;
    int i = 1;
    while (current != NULL) {
        printf("%d. %s\n", i++, current->command);
        current = current->next;
    }
}

void clear_cmd_list(CmdList* list) {
    CmdNode* current = list->head;
    while (current != NULL) {
        CmdNode* temp = current;
        current = current->next;
        free(temp);
    }
    list->head = NULL;
    list->count = 0;
}



